#!/bin/bash
set -e
HERE=`pwd`
CLOONIX_COMMON=${HERE}/../../../cloonix/common
TARGET=${CLOONIX_COMMON}/spice/spice_lib
if [ ! -d ${TARGET} ]; then
  echo
  echo NOT FOUND:
  echo ${TARGET}
  echo
  exit
fi
cd ${HERE}/spice_client
./patched_create
mv tainted_spice spice_client
rm -rf ${TARGET}/spice_client
mv spice_client ${TARGET}
cd ${TARGET}/spice_client/tests
patch meson.build ${HERE}/tests_meson.build.patch
cd ${TARGET}/spice_client
sed -i s/UNKNOWN/0.37/ ./build-aux/git-version-gen
sed -i s"%'-Wextra',%'-Wextra',\n'-Wl,-rpath',\n'-Wl,/usr/local/bin/cloonix/common/spice/spice_lib',%" meson.build
export PKG_CONFIG_PATH=${TARGET}/pkgconfig 
export DESTDIR=${TARGET}
meson build
cd build
ninja install
cp -rf ${TARGET}/usr/local/* ${TARGET}
rm -rf ${TARGET}/usr/local
if [ -d ${TARGET}/lib/x86_64-linux-gnu/pkgconfig ]; then
  mv ${TARGET}/lib/x86_64-linux-gnu/pkgconfig/* ${TARGET}/pkgconfig
  rmdir ${TARGET}/lib/x86_64-linux-gnu/pkgconfig
  mv ${TARGET}/lib/x86_64-linux-gnu/* ${TARGET}
  rmdir ${TARGET}/lib/x86_64-linux-gnu
  rmdir ${TARGET}/lib
else
  if [ -d ${TARGET}/lib64/pkgconfig ]; then
    mv ${TARGET}/lib64/pkgconfig/* ${TARGET}/pkgconfig
    rmdir ${TARGET}/lib64/pkgconfig
    mv ${TARGET}/lib64/* ${TARGET}
    rmdir ${TARGET}/lib64
  else
    echo ERROR NO:
    echo ${TARGET}/lib/x86_64-linux-gnu/pkgconfig
    echo ${TARGET}/lib64/pkgconfig
    exit 1
  fi
fi





